#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 1000
#define MAX_SUBJECTS 1000
#define MAX_NAME_LENGTH 101

typedef struct {
	char name[MAX_NAME_LENGTH];
	double weighted_avg;
} Student;

int compare(const void *a, const void *b) {
	const Student *s1 = (const Student *)a;
	const Student *s2 = (const Student *)b;
	if (s1->weighted_avg > s2->weighted_avg)
		return -1;
	if (s1->weighted_avg < s2->weighted_avg)
		return 1;
	return strcmp(s1->name, s2->name);
}

int main() {
	int T, n, m, i, j;
	T = 1;
	while (T--) {
		scanf("%d %d", &n, &m);
		int credits[m];
		for (i = 0; i < m; i++) {
			scanf("%d", &credits[i]);
		}

		Student students[n];
		for (i = 0; i < n; i++) {
			scanf("%100s", students[i].name);
			double sum_weighted_scores = 0.0;
			int sum_credits = 0;
			for (j = 0; j < m; j++) {
				int score;
				scanf("%d", &score);
				sum_weighted_scores += score * credits[j];
				sum_credits += credits[j];
			}
			students[i].weighted_avg = sum_weighted_scores / sum_credits;
		}

		qsort(students, n, sizeof(Student), compare);

		for (i = 0; i < n; i++) {
			printf("%s\n", students[i].name);
		}
		if (T > 0) {
			printf("\n"); // Print an extra newline between test cases
		}
	}
	return 0;
}